﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.cost
{
    /// <summary>  </summary>
    public class ShopRewardBll : IPageBll<ShopReward>
    {
        public string Title => "店铺返点";

        public bool EnableEditOnList => true;

        public bool EnableCreateOnList => true;

        public bool RefreshOnOpen => true;

        public Action<int> TurnPage { set; get; }
        public ShopReward CopyItem(ShopReward item)
        {
            if (item == null)
                return null;

            var newItem = item.Clone();
            newItem.id = 0;
            newItem.isdel = false;

            return newItem;
        }

        public ShopReward CreateItem()
        {
            return new ShopReward() { isdel = false, FUser = UserBll.CurrentUser.Name, FTime = DateTime.Now,};
        }

        public async Task<Result> DeleteItem(int id)
        {
            await DB.FSql.Update<ShopReward>()
                .Where(a => a.id == id && a.isdel == false)
                .Set(a => a.isdel, true)
                .ExecuteAffrowsAsync();

            await DB.FSql.Insert(new CLogs()
            {
                DocName = nameof(ShopReward),
                Computer = UserBll.CurrentUser.ComputerInfo,
                Zdr = UserBll.CurrentUser.Name,
                DocId = id.ToString(),
                DocAction = "删除",
            })
                .ExecuteAffrowsAsync();

            return new Result(true);
        }


        public async Task<Result<int>> EditCell(string dataPropertyName, object value,  ShopReward data)
        {
            if (data.id == 0)
            {
                data.FTime = await DB.FSql.GetTimeAsync();
                data.FUser = UserBll.CurrentUser.Name;
                data.isdel = false;

                var nid = await DB.FSql
                    .Insert(data)
                    .ExecuteIdentityAsync();

                data.id = (int)nid;
            }

            var dbRes = await DB.FSql.Update<ShopReward>()
                .Where(a => a.id == data.id && a.isdel == false)
                .SetDto(new Dictionary<string, object> { [dataPropertyName] = value })
                .ExecuteAffrowsAsync();

            return new Result<int>(true, null, data.id);
        }

        public IEnumerable<DeclareColumn> GetColumns()
        {
            return new[]
            {
                new DeclareColumn(nameof(ShopReward.id),"id"){ Visible= false},
                new DeclareColumn(nameof(ShopReward.shop),"店铺名称"),
                new DeclareColumn(nameof(ShopReward.reward),"返点"){ ValueType= typeof(decimal), Format="P2" },
                new DeclareColumn(nameof(ShopReward.FUser),"制单人"),
            };
        }

        public async Task<ShopReward> GetItem(int id)
        {
            return await DB.FSql.Select<ShopReward>()
                .Where(a => a.isdel == false && a.id == id)
                .ToOneAsync();
        }


        #region 查询参数




        #endregion

        /// <summary> 查询ui </summary>
        public virtual ControlBuilder GetUIBuilder()
        {
            return new ControlBuilder()
                .AddQueryButton("查询");

        }

        /// <summary> 查询 </summary>
        public async Task<PageQueryResult<ShopReward>> QueryPage(DocFilterParameter queryPara)
        {

            var list = await DB.FSql.Select<ShopReward>()
                .Where(a => a.isdel == false)
                .Page(queryPara.PageNo, queryPara.PageSize)
                .OrderByDescending(a => a.id)
                .Count(out var c)
                .ToListAsync();

            return new PageQueryResult<ShopReward>(true, null, list, c);
        }



        public async Task<Result> SaveItem(ShopReward item)
        {
            if (item == null)
            {
                return new Result(false, "系统错误");
            }


            string err = string.Empty;
            if (string.IsNullOrEmpty(item.shop))
            {
                err += "店铺名称不能为空!";
            }


            if (string.IsNullOrEmpty(err) == false)
            {
                return new Result(false, err);
            }

            item.isdel = false;
            item.FTime = await DB.FSql.GetTimeAsync();
            item.FUser = UserBll.CurrentUser.Name;

            if (item.id <= 0)
            {
                var id = await DB.FSql.Insert(item)
                    .ExecuteIdentityAsync();

                item.id = (int)id;
            }
            else
            {
                await DB.FSql.Update<ShopReward>()
                    .SetSource(item)
                    .ExecuteAffrowsAsync();
            }

            return new Result(true);

        }


        public async Task<Result> SaveItem(IEnumerable<ShopReward> items)
        {
            if (items == null)
            {
                return new Result(false, "系统错误");
            }


            var time = await DB.FSql.GetTimeAsync(); 

            foreach (var item in items)
            {
                item.isdel = false;
                item.FTime = time;
                item.FUser = UserBll.CurrentUser.Name;
            }

            await DB.FSql.Insert(items).ExecuteAffrowsAsync();

            return new Result(true);

        }


    }
}
